<?php






/**
 * ejecuta un comando en el shell y regresa el resultado del comando 
 * @param string $command
 * @return string|array 
 */
function shellCommand($command)
{
    #echo $command, "\n";
    $name = tempnam('/tmp', 'nsvn');
    exec($command.' > '.$name);
    exec($command.' 2>> '.$name);
    $content = file($name);
    unlink($name);
    #$content = (count($content) == 1) ? $content[0] : ( (count($content) == 0) ? '' : $content ) ;
    return $content;
}

function newTempFile($content)
{
    $name = tempnam('/tmp', 'nsvn');
    $handle = fopen($name, "w");
    fwrite($handle, $content);
    fclose($handle);
    return $name;
}

function isTextPlain($file)
{
   
   $url = parse_url($file);
   $name = basename($url['path']);
   $info = shellCommand("svn export '{$file}' /tmp/{$name}");
   $info = shellCommand("file -i /tmp/{$name}");
   $type = explode(':',$info[0]);
   $type =  trim($type[1]);
   if(eregi('text',$type))
      return true;
   else
      return false;
}


function getSvnFileProperties($file)
{
   global $svnAuth;
   $props = array();
   $properties = shellCommand("svn proplist '{$file}' {$svnAuth}");
   if(!eregi('^Prop',$properties['0']) )
      return $props;
   unset($properties[0]);
  
   foreach ( $properties as $prop )
   {
      $values = array();
      $prop = trim($prop);
      $command = "svn propget {$prop} '{$file}' {$svnAuth}";
      $propValues = shellCommand($command);
      foreach ($propValues as $value)
      {
         $values[] = trim($value);
      }
      $props[$prop] = $values;
   }
   return $props;
}

/**
 * Get the svn File info 
 * @param string $fileURL
 * @return Array
 */
function getSvnFileInfo($file)
{
   global $svnAuth;
   $return = array();
   $info = shellCommand("svn info '{$file}' {$svnAuth}");
   foreach( $info as $string )
   {
        @list($name,$value) = explode(':',$string,2);
        if(!trim($name)) continue;
        $return[ getKeyName($name) ] = trim($value);
   }
   
   $return['raw'] = getUpDir($return['url']).'/'.$return['name'];
   $return['last-changed-date'] = parseSvnDate($return['last-changed-date']);
   return $return;
}

/**
 * Convierte un string a un nombre valido de llave
 * @param string $name
 * @return string
 */
function getKeyName($name)
{
    $name = trim($name);
    $name = strtolower($name);
    $name = str_replace(' ','-',$name);
    return $name;
}


function parseSvnDate($date, $format = 'M d, Y')
{
   $date = trim($date);
   $date = eregi_replace(" \([A-Z][a-z]{1,4}, [0-9]{2} [A-Z][a-z]{1,4} [0-9]{4}\)$",'',$date);
   $time = strtotime($date);   
   return  date($format,$time);
}


/**
 * Get a list of file's previous revisions
 * @param string $file
 * @return Array
 */ 
function getPreviousRevisions($file)
{
  global $svnAuth;
  $command = "svn log {$file} {$svnAuth} | grep \"^r[1-9]\" ";
  $revisions = shellCommand($command);
  
  $revisions = array_reverse($revisions);
  $prev = 0;
  $revs = array();
  foreach ($revisions as $revision)
  {
    @list($rev, $by, $on ) = explode('|',$revision);
    $on = parseSvnDate($on);
    $revs[] = array( 'rev' => trim($rev), 'by' => trim($by), 'on' => $on, 'prev' => $prev );
    $prev = trim($rev);
  }
  $revs = array_reverse($revs);
  return $revs;
}



function getUpDir($file)
{
   $url = parse_url($file);
   $dir = dirname($url['path']);
   return $dir;
}





